# -*- coding: utf-8 -*-

from flask import session
from app.model.common.member import Member
from app.inc.functions import md5, rt


class Auth:
    def login(self, post):
        mb = Member.query.filter_by(email=post['username']).first()
        # 是否存在
        if mb is None:
            return rt(1, '账号不存在')
        else:
            # 密码是否正确
            if md5(post['password']) != mb.pwd:
                return rt(1, '密码错误')
        self.re_session(mb)
        return rt(0, 'ok')

    @staticmethod
    def re_session(mb):
        session['member_id'] = mb.member_id
        session['real_name'] = mb.real_name
        session['email'] = mb.email
        session['phone'] = mb.phone
        session['tel'] = mb.tel

    @staticmethod
    def logout():
        session.pop('member_id')
        session.pop('real_name')
        session.pop('email')
        session.pop('phone')
        session.pop('tel')
